package cmd

import (
	"encoding/json"
	"fmt"
	"github.com/CIPFZ/collector/internal/types"

	"github.com/CIPFZ/collector/internal/app"
	"github.com/CIPFZ/collector/pkg/version"
	"github.com/spf13/cobra"
)

// NewVersionCommand 创建 version 命令
func NewVersionCommand(collector *app.Collector) *cobra.Command {
	var opts *types.VersionOptions

	cmd := &cobra.Command{
		Use:   "version",
		Short: "Show version information",
		Long: `Display version information and build details.

Examples:
  collector version                    # Show full version information
  collector version --short           # Show short version only
  collector version --json            # Show version in JSON format`,
		Run: func(cmd *cobra.Command, args []string) {
			versionInfo := version.GetVersion()

			if opts.JsonOutput {
				data, _ := json.MarshalIndent(versionInfo, "", "  ")
				fmt.Println(string(data))
				return
			}

			if opts.Short {
				fmt.Println(versionInfo.ShortString())
				return
			}

			fmt.Println(versionInfo.String())
		},
	}

	cmd.Flags().BoolVar(&opts.Short, "short", false,
		"show short version only")
	cmd.Flags().BoolVar(&opts.JsonOutput, "json", false,
		"output version in JSON format")

	return cmd
}
